package com.ruoyi.web.controller.consume;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.consume.domain.SeedConsume;
import com.ruoyi.consume.domain.SimpleConsume;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consume.domain.FertilizerConsume;
import com.ruoyi.consume.service.IFertilizerConsumeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 查看领用信息Controller
 *
 * @author ruoyi
 * @date 2021-07-23
 */
@Api("查看领用信息")
@RestController
@RequestMapping("/consume/fertilizercon")
public class FertilizerConsumeController extends BaseController
{
    @Autowired
    private IFertilizerConsumeService fertilizerConsumeService;

    /**
     * 查询查看领用信息列表
     */
    @ApiOperation("查询查看领用信息列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:fertilizercon:list')")
    @GetMapping("/list")
    public TableDataInfo list(FertilizerConsume fertilizerConsume)
    {
        startPage();
        List<FertilizerConsume> list = fertilizerConsumeService.selectFertilizerConsumeList(fertilizerConsume);
        return getDataTable(list);
    }

    /**
     * 导出查看领用信息列表
     */
    @ApiOperation("导出查看领用信息列表")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:fertilizercon:export')")
    @Log(title = "查看领用信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FertilizerConsume fertilizerConsume)
    {
        List<FertilizerConsume> list = fertilizerConsumeService.selectFertilizerConsumeList(fertilizerConsume);
        ExcelUtil<FertilizerConsume> util = new ExcelUtil<FertilizerConsume>(FertilizerConsume.class);
        return util.exportExcel(list, "查看领用信息数据");
    }

    /**
     * 获取查看领用信息详细信息
     */
    @ApiOperation("获取查看领用信息详细信息")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:fertilizercon:query')")
    @GetMapping(value = "/{fertilizerConsumeId}")
    public AjaxResult getInfo(@PathVariable("fertilizerConsumeId") Long fertilizerConsumeId)
    {
        return AjaxResult.success(fertilizerConsumeService.selectFertilizerConsumeById(fertilizerConsumeId));
    }

    /**
     * 新增查看领用信息
     */
    @ApiOperation("新增查看领用信息")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:fertilizercon:add')")
    @Log(title = "查看领用信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FertilizerConsume fertilizerConsume)
    {
        return toAjax(fertilizerConsumeService.insertFertilizerConsume(fertilizerConsume));
    }

    /**
     * 修改查看领用信息
     */
    @ApiOperation("修改查看领用信息")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:fertilizercon:edit')")
    @Log(title = "查看领用信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FertilizerConsume fertilizerConsume)
    {
        return toAjax(fertilizerConsumeService.updateFertilizerConsume(fertilizerConsume));
    }

    /**
     * 删除查看领用信息
     */
    @ApiOperation("删除查看领用信息")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:fertilizercon:remove')")
    @Log(title = "查看领用信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fertilizerConsumeIds}")
    public AjaxResult remove(@PathVariable Long[] fertilizerConsumeIds)
    {
        return toAjax(fertilizerConsumeService.deleteFertilizerConsumeByIds(fertilizerConsumeIds));
    }

    /**
     * 获取用户列表下拉框
     */
    @ApiOperation("获取用户列表下拉框")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('consume:fertilizercon:username')")
    @GetMapping("/username")
    public TableDataInfo getUserName(){

        List<FertilizerConsume> list = fertilizerConsumeService.getUserName();
        List<SimpleConsume> simpleConsumes = new ArrayList<>();
        for (FertilizerConsume fertilizerConsume:list) {
            Long id = fertilizerConsume.getUserId();
            String name = fertilizerConsume.getUserName();
            SimpleConsume consume = new SimpleConsume(id,name);
            simpleConsumes.add(consume);
        }
        return getDataTable(simpleConsumes);
    }
}
